package com.jiangyg.mall.admin.controller;

import com.jiangyg.mall.admin.constant.AdminConstant;
import com.jiangyg.mall.admin.dto.UserDTO;
import com.jiangyg.mall.admin.dto.UserResourceDTO;
import com.jiangyg.mall.admin.vo.RoleVO;
import com.jiangyg.mall.admin.vo.UserVO;
import com.jiangyg.mall.admin.service.RoleService;
import com.jiangyg.mall.admin.service.UserService;
import com.jiangyg.mall.authz.UserInfoHolder;
import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.core.utils.I18n;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类描述：用户信息管理控制器
 *
 * @author jiangyg
 * @version 2.0
 * @date 2020-05-31
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private final RoleService roleService;

    private final UserService userService;

    @Autowired
    public UserController(UserService userService, RoleService roleService) {
        this.userService = userService;
        this.roleService = roleService;
    }

    /**
     * 功能描述：用户信息分页查询
     *
     * @param request    请求
     * @param pagination 分页参数
     * @return 返回分页用户信息
     * @throws Exception 异常
     */
    @ResponseBody
    @PostMapping("/select")
    public Result<Pagination<UserVO>> select(HttpServletRequest request, @RequestBody Pagination<UserVO> pagination) throws Exception {
        return Result.ok(userService.selectPagination(pagination, false));
    }

    /**
     * 功能描述：简单用户信息分页查询
     *
     * @param request    请求
     * @param pagination 分页参数
     * @return 返回分页用户信息
     * @throws Exception 异常
     */
    @ResponseBody
    @PostMapping("/select-simple")
    public Result<Pagination<UserVO>> simpleSelect(HttpServletRequest request, @RequestBody Pagination<UserVO> pagination) throws Exception {
        return Result.ok(userService.selectPagination(pagination, true));
    }

    /**
     * 功能描述：保存用户权限信息
     *
     * @param request 请求
     * @param dto     用户信息
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/save-permission")
    public Result<Object> savePermission(HttpServletRequest request, @RequestBody @Validated UserResourceDTO dto) {
        // 超级管理员账号只能自己修改保存
        if (UserInfoHolder.isAdmin() && !UserInfoHolder.isAdmin()) {
            return Result.error(I18n.getString("mall.admin.super.user.update"));
        }
        return Result.saveOrUpdate(userService.savePermission(dto));
    }

    /**
     * 功能描述：保存或者修改用户信息
     *
     * @param request 请求
     * @param dto     用户信息
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Object> saveOrUpdate(HttpServletRequest request, @RequestBody @Validated UserDTO dto) throws Exception {
        return Result.saveOrUpdate(userService.saveOrUpdate(dto));
    }

    /**
     * 功能描述：根据ID获取用户信息
     *
     * @param request 请求
     * @param userId  用户ID
     * @return 结果
     * @throws Exception 异常
     */
    @ResponseBody
    @PostMapping("/info")
    public Result<Object> info(HttpServletRequest request,
                               @RequestParam(name = "userId") Long userId) throws Exception {
        final Map<String, Object> data = new HashMap<>();
        // 用户信息
        final UserDTO user = userService.selectById(userId);
        data.put("user", user);
        // 用户的角色列表
        final List<String> ids = userService.selectRoleIdsById(userId);
        data.put("ids", ids);
        // 有权限查看的所有角色信息
        final Long loginUserId = UserInfoHolder.loginUserId();
        data.put("roleList", roleService.selectRoleList(loginUserId));
        return Result.ok(data);
    }

    /**
     * 功能描述：有权限查看的所有角色信息
     *
     * @param request 请求
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/roles")
    public Result<List<RoleVO>> selectRoleList(HttpServletRequest request) throws Exception {
        final Long userId = UserInfoHolder.loginUserId();
        return Result.ok(roleService.selectRoleList(userId));
    }

    /**
     * 功能描述：删除用户信息
     *
     * @param request 请求
     * @param userIds 批量删除的用户ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Object> delete(HttpServletRequest request, @RequestBody List<Long> userIds) {
        // 超级管理员不能删除
        if (userIds.contains(AdminConstant.SUPER_ADMIN_ID)) {
            return Result.error(I18n.getString("mall.admin.super.user.delete"));
        }
        return Result.delete(userService.delete(userIds));
    }

}
